package com.splant.android.smart.UILevel.Community;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.ModelLevel.DetectorListBean;
import com.splant.android.smart.ModelLevel.DeviceListBean;
import com.splant.android.smart.ModelLevel.WaterListBean;
import com.splant.android.smart.UILevel.Community.fragments.Garden_DetectorList_Adapter;
import com.splant.android.smart.UILevel.Community.fragments.Garden_WaterList_Adapter;
import org.apache.http.Header;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class Garden_DeviceList_Search_Activity extends Activity implements View.OnClickListener{


    private EditText edt_searchKey;
    private TextView tv_cancel;
    private String keyword="";
    private sPlantAsyncHttpClient sPlantClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.garden_waterlist_search_activity);

        tv_cancel= (TextView) findViewById(R.id.tv_cancel);
        edt_searchKey= (EditText) findViewById(R.id.edt_searchKey);

        lrv_waterList= (LRecyclerView) findViewById(R.id.lrv_waterList);
        lrv_detectorList= (LRecyclerView) findViewById(R.id.lrv_detectorList);

        sPlantClient = sPlantAsyncHttpClient.getInstance();

        init();

        edt_searchKey.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    keyword=v.getText().toString();
                    if (keyword.equals("")==false)
                    {
                        waterList.clear();
                        detectorList.clear();
                        getDatas();
                    }
                }
                    return true;
            }
        });
        tv_cancel.setOnClickListener(this);
    }

    private LRecyclerView lrv_waterList;
    private LRecyclerViewAdapter waterListlRecyclerViewAdapter;
    private Garden_WaterList_Adapter garden_waterList_adapter;
    private List<WaterListBean.Water> waterList = new ArrayList<>();

    private LRecyclerView lrv_detectorList;
    private LRecyclerViewAdapter detectorListlRecyclerViewAdapter;
    private Garden_DetectorList_Adapter garden_detectorList_adapter;
    private List<DetectorListBean.Detector> detectorList = new ArrayList<>();

    private void init()
    {
        garden_waterList_adapter = new Garden_WaterList_Adapter(this);
        waterListlRecyclerViewAdapter = new LRecyclerViewAdapter(garden_waterList_adapter);

        lrv_waterList.setHasFixedSize(true);
        lrv_waterList.setLayoutManager(new GridLayoutManager(this, 1));
        lrv_waterList.setAdapter(waterListlRecyclerViewAdapter);
        lrv_waterList.setRefreshProgressStyle(ProgressStyle.BallClipRotateMultiple);

        GridItemDecoration divider = new GridItemDecoration.Builder(this)
                .setVertical(R.dimen.gaiaa_devicelist_dh)
                .setColorResource(R.color.garden_color0)
                .build();
        lrv_waterList.addItemDecoration(divider);

        lrv_waterList.setPullRefreshEnabled(false);
        lrv_waterList.setLoadMoreEnabled(false);

        garden_waterList_adapter.setOnItemClickListener(new Garden_WaterList_Adapter.onItemClickListener() {
            @Override
            public void onItemChick(int pos) {

                WaterListBean.Water water= garden_waterList_adapter.getDataList().get(pos);

                Intent intent=new Intent(Garden_DeviceList_Search_Activity.this,Garden_Water_Activity.class);
                intent.putExtra("water",water);

                startActivity(intent);

            }
        });

        garden_detectorList_adapter = new Garden_DetectorList_Adapter(this);
        detectorListlRecyclerViewAdapter = new LRecyclerViewAdapter(garden_detectorList_adapter);

        lrv_detectorList.setHasFixedSize(true);
        lrv_detectorList.setLayoutManager(new GridLayoutManager(this, 1));
        lrv_detectorList.setAdapter(detectorListlRecyclerViewAdapter);
        lrv_detectorList.setRefreshProgressStyle(ProgressStyle.BallClipRotateMultiple);

        lrv_detectorList.addItemDecoration(divider);

        lrv_detectorList.setPullRefreshEnabled(false);
        lrv_detectorList.setLoadMoreEnabled(false);

        garden_detectorList_adapter.setOnItemClickListener(new Garden_DetectorList_Adapter.onItemClickListener() {
            @Override
            public void onItemChick(int pos) {

                Intent intent = new Intent(Garden_DeviceList_Search_Activity.this, Garden_Detector_Activity.class);

                intent.putExtra("detector", garden_detectorList_adapter.getDataList().get(pos));

                startActivity(intent);

            }
        });

    }

    private void getDatas()
    {
        RequestParams params = new RequestParams();
        params.put("keyword", keyword);

        sPlantClient.get(sPlantAsyncHttpClient.GETSEARCHDEVICES, params, new sPlantJsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try {
                    if (response.getInt("succeed") == 1) {
                        Gson gson = new Gson();

                        DeviceListBean deviceListBean = gson.fromJson(response.toString(), DeviceListBean.class);

                        if (deviceListBean.getWaterList()!= null) {

                            waterList.addAll(deviceListBean.getWaterList());

                            garden_waterList_adapter.setDataList(waterList);
                        }

                        if (deviceListBean.getDetectorList()!=null)
                        {
                           detectorList.addAll(deviceListBean.getDetectorList());
                            garden_detectorList_adapter.setDataList(detectorList);
                        }

                        if ((deviceListBean.getWaterList()==null||deviceListBean.getWaterList().size()==0)&&(deviceListBean.getDetectorList()==null||deviceListBean.getWaterList().size()==0))
                        {
                           Toast.makeText(Garden_DeviceList_Search_Activity.this,"未搜索到相关节点",Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (Exception e) {

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {

                Toast.makeText(Garden_DeviceList_Search_Activity.this, getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.tv_cancel:
                finish();
                break;
        }
    }
}
